
import { Controller, Inject, Get, Query, Param, Del, Post, Put, Body, createMiddleware, Files } from '@midwayjs/core';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@midwayjs/swagger';
import { Auth } from "../decorator/auth.decorator";
import { BusinessType, Log } from "../decorator/log.decorator";
import { TeacherService } from '@service/teacher.service';
import { CreateTeacherDto, UpdateTeacherDto, QueryTeacherDto } from '@dto/teacher.dto';
import { ExitTeamDto } from '@dto/team.dto';
import { UploadMiddleware, UploadStreamFileInfo } from '@midwayjs/busboy';

@ApiTags('教师管理')
@Controller('system/teacher')
export class TeacherController {
  @Inject()
  teacherService: TeacherService;
  // 创建
  @ApiOperation({ summary: '创建教师信息', description: '创建新的教师信息记录' })
  @ApiBody({ type: CreateTeacherDto, description: '教师信息数据' })
  @ApiResponse({ status: 200, description: '创建成功' })
  @Auth('system:teacher:add')
  @Log({ title: '教师信息表-创建', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() body: CreateTeacherDto) {
    return await this.teacherService.create(body);
  }
  // 获取列表
  @ApiOperation({ summary: '获取教师列表', description: '分页查询教师信息列表' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:teacher:query')
  @Log({ title: '教师信息表-列表', businessType: BusinessType.LIST })
  @Get('/list')
  async list(@Query() query: QueryTeacherDto) {
    return await this.teacherService.list(query);
  }

  // 根据学院ID获取教师列表
  @ApiOperation({ summary: '根据学院ID获取教师列表', description: '根据学院ID查询该学院下的所有教师' })
  @ApiParam({ name: 'deptId', description: '学院ID', type: 'number' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:teacher:query')
  @Log({ title: '教师信息表-按学院查询', businessType: BusinessType.LIST })
  @Get('/listByDept/:deptId')
  async listByDept(@Param('deptId') deptId: string) {
    return await this.teacherService.listByDept(+deptId);
  }
  // 获取详情
  @ApiOperation({ summary: '获取教师详情', description: '根据教师ID获取教师详细信息' })
  @ApiParam({ name: 'teacherId', description: '教师ID', type: 'string' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:teacher:query')
  @Log({ title: '教师信息表-详情', businessType: BusinessType.DETAIL })
  @Get('/:teacherId')
  async detail(@Param('teacherId') teacherId: string) {
    return await this.teacherService.detail(teacherId);
  }
  // 修改
  @ApiOperation({ summary: '更新教师信息', description: '更新教师信息记录' })
  @ApiBody({ type: UpdateTeacherDto, description: '更新的教师信息数据' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Auth('system:teacher:edit')
  @Log({ title: '教师信息表-修改', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() body: UpdateTeacherDto) {
    return await this.teacherService.update(body);
  }
  // 教师更新自己信息
  @ApiOperation({ summary: '教师更新自己信息', description: '更新教师自己的信息记录' })
  @ApiBody({ type: UpdateTeacherDto, description: '更新的教师信息数据' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Log({ title: '教师信息表-修改', businessType: BusinessType.EDIT })
  @Put('/info')
  async updateInfo(@Body() body: UpdateTeacherDto) {
    return await this.teacherService.update(body);
  }
  // 删除
  @ApiOperation({ summary: '删除教师信息', description: '根据教师ID删除教师信息，支持批量删除' })
  @ApiParam({ name: 'teacherId', description: '教师ID，多个ID用逗号分隔', type: 'string' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Auth('system:teacher:remove')
  @Log({ title: '教师信息表-删除', businessType: BusinessType.REMOVE })
  @Del('/:teacherId')
  async remove(@Param('teacherId') teacherId: string) {
    const teacherIds = teacherId.split(',').map((teacherId) => +teacherId);
    return this.teacherService.remove(teacherIds);
  }
  // 导出
  @ApiOperation({ summary: '导出教师信息', description: '根据查询条件导出教师信息到Excel文件' })
  @ApiBody({ type: QueryTeacherDto, description: '查询条件' })
  @ApiResponse({ status: 200, description: '导出成功' })
  @Auth('system:teacher:export')
  @Log({ title: '教师信息表-导出', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Body() queryParams: QueryTeacherDto) {
    return await this.teacherService.export(queryParams);
  }
   //导出模板
    @ApiOperation({ summary: '导出教师信息模板', description: '导出教师信息的Excel模板文件' })
    @ApiResponse({ status: 200, description: '导出成功' })
    @Log({ title: '教师信息表-导出模板', businessType: BusinessType.EXPORT })
    @Post('/exportTemplate')
    async exportTemplate() {
      return await this.teacherService.importTemplate();
    }
    // 导入模板数据
    @ApiOperation({ summary: '导入教师信息模板数据', description: '根据导入的Excel文件导入教师信息' })
    @ApiBody({ type: FormData, description: '导入的Excel文件' })
    @ApiResponse({ status: 200, description: '导入成功' })
    @Auth('system:teacher:import')
    @Log({ title: '教师信息表-导入', businessType: BusinessType.IMPORT })
    @Post('/importData', {
      middleware: [createMiddleware(UploadMiddleware, { mode: 'stream' })]
    })
    async importData(@Files() files: UploadStreamFileInfo,
      @Query('updateSupport') updateSupport: boolean) {
      return await this.teacherService.importData({files, updateSupport});  
    }
    // 学生退出队伍
    @ApiOperation({ summary: '学生退出队伍', description: '根据teamId和studentId将学生从队伍中移除' })
    @ApiBody({ type: ExitTeamDto, description: '队伍ID和学生ID' })
    @ApiResponse({ status: 200, description: '退出成功' })
    @Log({ title: '队伍管理-学生退出', businessType: BusinessType.REMOVE })
    @Post('/exitTeam')
    async exitTeam(@Body() body: ExitTeamDto) {
      return await this.teacherService.exitTeam(body);
    }   
}